package com.wangcang.widgetlibrary.textinput;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;

/**
 * @author : mundane
 * @time : 2017/6/21 15:06
 * @description :
 * @file : CountTextLayout.java
 */

public class CountTextLayout extends LinearLayout {

	private LinearLayout mIndicatorArea;
	private TextView mCounterView;
	private FrameLayout mInputFrame;
	private EditText mEditText;
	private int mRequiredTextCount;
	private final int DEF_COUNT_VIEW_RIGHT_MARGIN = 16;
	private final int DEF_COUNT_VIEW_BOTTOM_MARGIN = 16;
	private final int DEF_EDIT_TEXT_PADDING_LEFT = 16;
	private final int DEF_EDIT_TEXT_PADDING_TOP = 20;
	//	默认指示器文字大小为12sp
	private final int DEF_COUNT_VIEW_TEXT_SIZE = 12;
	private final int NORMAL_COLOR = getResources().getColor(R.color.common_color_c8_d5d7dc);
	private final int OVER_FLOW_COLOR = getResources().getColor(R.color.common_color_c11_ff1f1f);

	public CountTextLayout(Context context) {
		this(context, null);
	}

	public CountTextLayout(Context context, @Nullable AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CountTextLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initAttrs(context, attrs);
		initData(context);
		setBackgroundColor(Color.parseColor("#33d5d7dc"));
	}

	private void initData(Context context) {
		setOrientation(VERTICAL);
		setBackgroundResource(R.drawable.shape_round_corner);
		mInputFrame = new FrameLayout(context);
		addView(mInputFrame);
		mIndicatorArea = new LinearLayout(context);
		mIndicatorArea.setOrientation(HORIZONTAL);
		addView(mIndicatorArea, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		Space space = new Space(context);
		LayoutParams spaceParams = new LayoutParams(0, 0, 1f);
		mIndicatorArea.addView(space, spaceParams);
		mCounterView = new TextView(context);
		mCounterView.setText(String.format("0/%d", mRequiredTextCount));
		mCounterView.setTextColor(NORMAL_COLOR);
		mCounterView.setTextSize(TypedValue.COMPLEX_UNIT_SP, DEF_COUNT_VIEW_TEXT_SIZE);
		LayoutParams countViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		countViewParams.rightMargin = dp2px(DEF_COUNT_VIEW_RIGHT_MARGIN);
		countViewParams.bottomMargin = dp2px(DEF_COUNT_VIEW_BOTTOM_MARGIN);
		mIndicatorArea.addView(mCounterView, countViewParams);

	}

	@Override
	public void addView(View child, int index, ViewGroup.LayoutParams params) {
		if (child instanceof EditText) {
			mEditText = (EditText) child;
			mEditText.setGravity(Gravity.LEFT);
			mEditText.setPadding(dp2px(DEF_EDIT_TEXT_PADDING_LEFT), dp2px(DEF_EDIT_TEXT_PADDING_TOP), dp2px(DEF_EDIT_TEXT_PADDING_LEFT), 0);
			mEditText.addTextChangedListener(new TextWatcher() {
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count, int after) {

				}

				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
					int length = s.length();
					if (length > mRequiredTextCount) {
						mCounterView.setText(String.format("超出%d个字", length - mRequiredTextCount));
						mCounterView.setTextColor(OVER_FLOW_COLOR);
					} else {
						mCounterView.setText(String.format("%d/%d", length, mRequiredTextCount));
						mCounterView.setTextColor(NORMAL_COLOR);
					}
				}

				@Override
				public void afterTextChanged(Editable s) {

				}
			});
			mInputFrame.addView(mEditText, new FrameLayout.LayoutParams(params));
			mEditText.setBackgroundDrawable(null);
			// Now use the EditText's LayoutParams as our own and update them to make enough space for the label
			mInputFrame.setLayoutParams(params);
			mInputFrame.setPadding(0, 0, 0, 0);
		} else {
			super.addView(child, index, params);
		}
	}

	private void initAttrs(Context context, AttributeSet attrs) {
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CountTextLayout);
		mRequiredTextCount = a.getInt(R.styleable.CountTextLayout_text_count, 0);
		a.recycle();
	}

	public int px2dp(float paValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (paValue / scale + 0.5f);
	}

	public int dp2px(float dpValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public int px2sp(float pxValue) {
		final float frontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (pxValue / frontScale + 0.5f);
	}

	public int sp2px(float spValue) {
		final float frontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * frontScale + 0.5f);
	}
}
